﻿Public Class clsInhPracownik
    Inherits clsPracownik

#Region "Attributes"

    Private mvarUprawnienia As Collection(Of clsInhUprawnienia)

#End Region

#Region "Properties"

    Public Property Uprawnienia() As Collection(Of clsInhUprawnienia)
        Get
            Return mvarUprawnienia
        End Get
        Set(ByVal value As Collection(Of clsInhUprawnienia))
            mvarUprawnienia = value
        End Set
    End Property

#End Region

#Region "Methods"

    Public Sub New()
        MyBase.New()
        mvarUprawnienia = New Collection(Of clsInhUprawnienia)
    End Sub

    Public Sub New(ByVal ID As Integer, ByVal lvarDbHolder As clsDataBase)

        MyBase.New(ID.ToString(), lvarDbHolder, VAR_ERROR_HANDLER)
        mvarUprawnienia = New Collection(Of clsInhUprawnienia)

        Dim lvarTabel As DataTable = clsPracownik.PobierzWszystkieUpawnienia(lvarDbHolder, VAR_ERROR_HANDLER, ID)
        If Not lvarTabel Is Nothing Then
            For Each lvarRow As DataRow In lvarTabel.Rows
                mvarUprawnienia.Add(New clsInhUprawnienia(CInt(lvarRow.Item("id")), lvarDbHolder))
            Next
        End If

    End Sub

    Public Overloads Sub Save(ByVal lvarDbHolder As clsDataBase)

        lvarDbHolder.BeginTrans()

        MyBase.Save(lvarDbHolder, VAR_ERROR_HANDLER)

        For Each lvarUprawnienia As clsUprawnienia In mvarUprawnienia
            lvarUprawnienia.PracownikId = CInt(MyBase.Id)
            lvarUprawnienia.Save(lvarDbHolder, VAR_ERROR_HANDLER)
        Next

        lvarDbHolder.CommitTrans()

    End Sub

    Shared Function SprawdzNumer(ByVal lvarDbHolder As clsDataBase, ByVal Zapytanie As Integer) As Integer

        Dim lvarZapytanie As String
        Dim lvarReturn As Integer

        lvarZapytanie = Zapytanie.ToString()

        Dim lvarReader As OdbcDataReader = Nothing
        If lvarDbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(lvarDbHolder, "queries_application_oem", "cls_pracownik", 1, VAR_ERROR_HANDLER)

            With lvarSqlRunner
                .AddParameter("p_zapytanie", Zapytanie.ToString())
                lvarReader = .ExecuteQuery
            End With

            If lvarReader.HasRows Then
                While lvarReader.Read
                    lvarReturn = CInt(IIf(IsDBNull(lvarReader.Item("id")), -1, lvarReader.Item("id")))
                End While
            End If

        End If
        Return lvarReturn

    End Function

    Shared Function SprawdzAktywnoscPracownika(ByVal lvarDbHolder As clsDataBase, ByVal IdPracownika As Integer) As Boolean

        Dim lvarReturn As Integer

        Dim lvarReader As OdbcDataReader = Nothing
        If lvarDbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(lvarDbHolder, "queries_application_oem", "cls_pracownik", 2, VAR_ERROR_HANDLER)

            With lvarSqlRunner
                .AddParameter("p_zapytanie", IdPracownika.ToString())
                lvarReader = .ExecuteQuery
            End With

            If lvarReader.HasRows Then
                While lvarReader.Read
                    lvarReturn = CInt(IIf(IsDBNull(lvarReader.Item("aktywny")), -1, lvarReader.Item("aktywny")))
                End While
            End If

        End If
        Return CBool(lvarReturn)

    End Function

#End Region

End Class
